//
//  JournalViewController.m
//  Kiosk
//
//  Created by Raffael Reichelt on 26.03.12.
//  Copyright (c) 2012 rare dad. All rights reserved.
//

#import "JournalViewController.h"
#import "SimpleTableCell.h"

@interface JournalViewController ()

@end

@implementation JournalViewController

@synthesize navBar = _navbar;
@synthesize searchBar = _searchBar;
@synthesize tableView = _tableView;

@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (Journal == nil) {
        Journal = [self.delegate getJournal];
    }
    
    self.navBar.topItem.title = NSLocalizedString(@"Journal", @"Journal Title");
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [[Journal objectForKey:@"Journal"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 	{
    
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    
    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.nameLabel.text = [[[Journal objectForKey:@"Journal"] objectAtIndex:indexPath.row]objectAtIndex:0];
    
    /*NSURL *myurl = [[NSURL alloc]initWithString:[[[Journal objectForKey:@"Journal"] objectAtIndex:indexPath.row]objectAtIndex:2]];
    NSURLRequest* request = [[NSURLRequest alloc]initWithURL:myurl];
    [cell.preview loadRequest:request];
    */
    cell.thumbnailView.image = [[[Journal objectForKey:@"Journal"] objectAtIndex:indexPath.row]objectAtIndex:1];

    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *journal = [[[Journal objectForKey:@"Journal"] objectAtIndex:indexPath.row]objectAtIndex:2];
    [self.delegate loadURL:journal];
    [self.delegate journalViewControllerDidFinish:self];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[Journal objectForKey:@"Journal"] removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (IBAction)done:(id)sender
{
    [self.delegate journalViewControllerDidFinish:self];
}


@end
